<h3>Syntax</h3>

Here is the syntax of important subprocess functions

<pre>
&lt;status> = subprocess.call         (&lt;command array>[, &lt;options>])
&lt;status> = subprocess.check_call   (&lt;command array>[, &lt;options>])
&lt;string> = subprocess.check_output (&lt;command array>[, &lt;options>])
&lt;Popen>  = subprocess.Popen        (&lt;command array>[, &lt;options>])
</pre>

<h3>Basic Example</h3>

<pre>import subprocess

subprocess.call(['find', '/etc', '-name', '*.conf'])
</pre>

In the example a program named 'find' is executed without a shell. To launch the command in a shell pass 'shell=True' in the list of options

<pre>subprocess.call(['find', '/'], shell=True)</pre>

<h3>Catching STDOUT</h3>

To collect all output of a command executed use check_output()

<pre>output = subprocess.check_output(["ls"])</pre>

<h3>Redirecting STDERR</h3>

When you only want to redirect STDERR to STDIN and catch this output too, simply add 'stderr=subprocess.STDOUT' as an option when calling check_output()

<pre>output = subprocess.check_output(["unknown_command"], stderr=subprocess.STDOUT)</pre>

<h3>Complex Pipe Use Cases</h3>

To also handle STDIN, STDOUT and STDERR you need use Popen() and Popen.communicate() to write and read from/to those pipes.

<pre>
# Launch command with all pipes connected
p = subprocess.Popen(['rm', '-i', '*'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Pass exactly one 'y' and read output
(out, err) = p.communicate('y')

# Check return code
if p.returncode != 0:
    print("Error!")
</pre>

Don't forget to pass a 'stdxx=subprocess.PIPE' option for each pipe you want to use

<h3>Passing Environment Variables</h3>

<pre>Popen(["ls"], env={"PATH": "/usr/local/bin"})</pre>
